/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.factory.web.validation;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import javassist.bytecode.stackmap.BasicBlock;
import org.jgrapht.experimental.dag.DirectedAcyclicGraph;
import org.jgrapht.graph.DefaultEdge;

/**
 *
 * @author milos
 */
public class CyclicDetector {

    public boolean isAcyclic(InputStream inputStream) {
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
            DirectedAcyclicGraph graph = new DirectedAcyclicGraph(DefaultEdge.class);

            String line = in.readLine();
            while (line != null) {
                String[] splittedLine = line.split(" ");
                String sourceNode = splittedLine[0];
                String targetNode = splittedLine[1];
                graph.addVertex(sourceNode);
                graph.addVertex(targetNode);
                try {
                    graph.addEdge(sourceNode, targetNode);
                } catch (Exception ex) {
                    return false;
                }
                line = in.readLine();
            }
            return true;
        } catch (Exception ex) {
            return false;
        }
    }
}
